package com.wtwd.device.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.device.dao.DialPlateMapper;
import com.wtwd.device.model.entity.DialPlate;
import com.wtwd.device.service.DialPlateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DialPlateServiceImpl implements DialPlateService {
    @Value( "${file.http}")
    private String fileHttp;
    @Value( "${dial.preview.image}")
    private String previewImg;
    @Value("${dial.file}")
    private String dialFile;
    @Autowired
    private DialPlateMapper dialPlateMapper;
    @Override
    public Map<String,Object> getDefaultDialPlateList(String id,Integer curPage,Integer pageSize) {
        Page<DialPlate> page=new Page<>(curPage,pageSize);
        IPage<DialPlate> result= dialPlateMapper.getDefaultDialPlateList(id,page);
        List<DialPlate> records = result.getRecords();
        records.stream().forEach(e->{
            if (StringUtils.isNotBlank(e.getPreviewImg())){
                String url=fileHttp.concat(previewImg.concat(e.getPreviewImg()));
                e.setPreviewImg(url);
            }
            if (StringUtils.isNotBlank(e.getDialFile())){
                String fileUrl=fileHttp.concat(dialFile.concat(e.getDialFile()));
                e.setDialFilePath(fileUrl);
            }
        });
        long total = result.getTotal();
        Map<String,Object> map=new HashMap<>();
        map.put("total",total);
        map.put("data",result.getRecords());
        return map;
    }

    @Override
    public Integer addDialPlate(DialPlate dialPlate) {
        return dialPlateMapper.addDialPlate(dialPlate);
    }

    @Override
    public Integer modifyDialPlate(DialPlate dialPlate) {
        return dialPlateMapper.modifyDialPlate(dialPlate);
    }

    @Override
    public Integer getMaxDialPlateOrder(String watchId) {
        return dialPlateMapper.getMaxDialOrder(watchId);
    }

    @Override
    public Integer getDialOrderByWatchIdAndOrder(String watchId, Integer order) {
        return dialPlateMapper.getDialOrderByWatchIdAndOrder(watchId, order);
    }

    @Override
    public Integer modifyDialOrderById(Integer id, Integer order) {
        return dialPlateMapper.modifyDialOrderById(id, order);
    }

    @Override
    public DialPlate getDialPlateById(Integer id) {
        return dialPlateMapper.getDialPlateById(id);
    }

    @Override
    public Integer deleteDialById(Integer id) {
        return dialPlateMapper.deleteDialById(id);
    }

    @Override
    public List<DialPlate> getDialOrderByWatchId(String watchId) {
        if (StringUtils.isBlank(watchId)){
            return null;
        }
        return dialPlateMapper.getDialOrderByWatchId(watchId);
    }
}
